﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using ConcurrencyAPI.Models;
using ConcurrencyAPI.ViewModels;
using ConcurrencyAPI.Repository;
using System.Transactions;

namespace ConcurrencyAPI.Controllers
{
    public class ProductsController : ApiController
    {
        private ProductRepository productRepository = new ProductRepository();
        // GET api/Products/Get
        public ReturnJsonInfo<List<Product>> Get()
        {
            ReturnJsonInfo<List<Product>> returnJson = new ReturnJsonInfo<List<Product>>();

            try
            {
                List<Product> userList = productRepository.GetAllProducts();
                if (userList.Count == 0)
                {
                    returnJson.status = 1;
                    returnJson.message = "no data";
                    returnJson.data = null;
                }
                else
                {
                    returnJson.status = 1;
                    returnJson.message = "get data success";
                    returnJson.data = userList;
                }
            }
            catch (Exception error)
            {
                returnJson.status = 0;
                returnJson.message = error.Message;
                returnJson.data = null;
            }
            return returnJson;
        }

        // GET api/Products/Get/5
        public ReturnJsonInfo<Product> Get(int id)
        {
            ReturnJsonInfo<Product> returnJson = new ReturnJsonInfo<Product>();
            try
            {
                Product product = productRepository.GetProduct(id);
                if (product == null)
                {
                    returnJson.status = 1;
                    returnJson.message = "no data";
                    returnJson.data = null;
                }
                else
                {
                    returnJson.status = 1;
                    returnJson.message = "get data success";
                    returnJson.data = product;
                }
            }
            catch (Exception error)
            {
                returnJson.status = 0;
                returnJson.message = error.Message;
                returnJson.data = null;
            }

            return returnJson;
        }

        // POST api/Products/Add
        [System.Web.Http.HttpPost]
        public ReturnJsonInfo<Product> Add(Product product)
        {
            ReturnJsonInfo<Product> returnJson = new ReturnJsonInfo<Product>();
            //利用RowVersion和事务来处理并发问题
            using (var ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    product.CreateTime = DateTime.Now;
                    var entity = productRepository.AddEntity(product);
                    if (entity != null)
                    {
                        returnJson.status = 1;
                        returnJson.message = "save data success";
                        returnJson.data = entity;
                    }
                    else
                    {

                        returnJson.status = 1;
                        returnJson.message = "fail to save data";
                        returnJson.data = null;
                    }
                    //如果没有发生异常，则事务完成
                    ts.Complete();
                }
                catch (Exception error)
                {
                    returnJson.status = 0;
                    returnJson.message = error.Message;
                    returnJson.data = null;
                }
            }
            return returnJson;
        }

        // POST api/Products/Update
        [System.Web.Http.HttpPost]
        public ReturnJsonInfo<Product> Update(Product product)
        {
            ReturnJsonInfo<Product> returnJson = new ReturnJsonInfo<Product>();
            using (var ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    bool result = productRepository.UpdateProduct(product);
                    //if updated data is same as old data, result is false
                    returnJson.status = 1;
                    returnJson.message = "update data success";
                    returnJson.data = product;
                    //如果没有发生异常，则事务完成
                    ts.Complete();
                }
                catch (Exception error)
                {
                    returnJson.status = 0;
                    returnJson.message = error.Message;
                    returnJson.data = null;
                }
            }

            return returnJson;
        }

        // DELETE api/Products/Delete
        [System.Web.Http.HttpPost]
        public ReturnJsonInfo<Product> Delete(Product product)
        {
            ReturnJsonInfo<Product> returnJson = new ReturnJsonInfo<Product>();
            using (var ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    bool result = productRepository.DeleteProduct(product.Id);
                    if (result)
                    {
                        returnJson.status = 1;
                        returnJson.message = "delete data success";
                        returnJson.data = null;
                    }
                    else
                    {

                        returnJson.status = 0;
                        returnJson.message = "fail to delete data";
                        returnJson.data = null;
                    }
                    //如果没有发生异常，则事务完成
                    ts.Complete();
                }
                catch (Exception error)
                {
                    returnJson.status = 0;
                    returnJson.message = error.Message;
                    returnJson.data = null;
                }
            }
            return returnJson;
        }

    }
}